package com.pxmeta.skill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pxmeta.skill.entity.SkillFormItem;
import com.pxmeta.skill.mapper.SkillFormItemMapper;
import com.pxmeta.skill.service.ISkillFormItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SkillFormItemServiceImpl extends ServiceImpl<SkillFormItemMapper, SkillFormItem> implements ISkillFormItemService {

    @Override
    public List<SkillFormItem> getItemListByFormId(Integer formId) {
        LambdaQueryWrapper<SkillFormItem> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SkillFormItem::getFormId, formId)
                .orderByAsc(SkillFormItem::getSort);

        return this.list(wrapper);
    }

    @Override
    public void saveBatchByFromId(List<String> names, List<String> values, Integer id) {

        List<SkillFormItem> finalList = Lists.newLinkedList();
        for (int i = 0; i < names.size(); i++) {
            String name = names.get(i);
            String value = null;
            if (values != null){
                 value = values.get(i);
            }


            SkillFormItem item = new SkillFormItem();
            item.setValue(value);
            item.setName(name);
            item.setFormId(id);
            item.setSort(i);
            item.setPid(0);
            finalList.add(item);
        }

        this.saveBatch(finalList);

    }
}
